package demo1;
/**
 * sychronized关键字
 * 对某个对象加锁
 */
public class T {

    private int count = 10;
    private Object o = new Object();

    public void m() {
        synchronized (o) {//任何线程要执行下面的代码，必须要先拿到o对象的锁
            count--;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.printf("%s count = %d%n", Thread.currentThread().getName(), count);
        }
    }


    public static void main(String[] args) {
        final T t = new T();
        Thread thread1 = new Thread(t::m);
        Thread thread2 = new Thread(t::m);
        thread1.start();
        thread2.start();
    }
}
